package com.example.config;

/**
 * @application:
 * @author: LiYuhang
 * @Date: 2021/6/2 21:39
 * @version: 0.1
 * @Description :
 * @Modified By:
 */

import com.example.properties.DemoStarterProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(DemoStarterProperties.class)
@ConditionalOnClass(DemoStarterProperties.class)
@ConditionalOnProperty(prefix = "com.example.starter.demo", value = "enabled", matchIfMissing = true)
public class DemoAutoConfiguration {


    /**
     * @Configuration：表明此类是一个配置类，将变为一个bean被spring进行管理。
     * @EnableConfigurationProperties：启用属性配置，将读取HelloServiceProperties里面的属性。
     * @ConditionalOnClass：当类路径下面有HelloServiceConfiguration此类时，自动配置。
     * @ConditionalOnProperty：判断指定的属性是否具备指定的值。
     * @ConditionalOnMissingBean：当容器中没有指定bean是，创建此bean。
     */

    @Autowired
    private DemoStarterProperties demoStarterProperties;

    @Bean
    @ConditionalOnMissingBean(DemoStarterProperties.class)
    public DemoStarterProperties demoStarterProperties() {
        DemoStarterProperties properties = new DemoStarterProperties();
        properties.setAge(demoStarterProperties.getAge());
        properties.setSex(demoStarterProperties.getSex());
        properties.setSingle(demoStarterProperties.getSingle());
        properties.setLocationAddress(demoStarterProperties.getLocationAddress());
        properties.setHobby(demoStarterProperties.getHobby());
        return properties;
    }

}
